package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

public class ClientConnection extends Thread {

	private int threadId;
	private Socket clientSocket;
	private boolean clientIsConnected;
	
	public ClientConnection(int threadId, Socket clientSocket) {
		this.threadId = threadId;
		this.clientSocket = clientSocket;
		this.clientIsConnected = true;
	}

    @Override
	public void run() {
		System.out.println("Client connected! Thread ID: " + threadId);

		String clientSentence;
	

		try {
			BufferedReader inFromClient;
			

			while(clientIsConnected) {
				// Is the socket disconnected?
				if(clientSocket.isOutputShutdown()) {
					clientIsConnected = false;
				} else {
					inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

					clientSentence = inFromClient.readLine();
					System.out.println("Thread ID: " + threadId + ", received message: " + clientSentence);
			
				}
			}
		} catch (SocketException e) {
			System.out.println("Thread ID: " + threadId + ", socket closed unexpectedly!");
		} catch (IOException e) {
		} finally {
			System.out.println("Thread ID: " + threadId + ", finished!");
		}
	}

	
    public Socket getClientSockeet() {
    	return clientSocket;
    }

	
	

}
